package com.xuetang9.controller.al.user;

import com.xuetang9.domain.User;
import com.xuetang9.dto.al.MsgDto;
import com.xuetang9.service.al.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author : allende
 * @describe : 用户修改删除冻结 控制器
 * @date : 2020/5/12 11:16
 * vision : 1.0.0
 **/
@Controller("UserControllerAL")
@CrossOrigin   //处理跨域请求问题
public class UserController {

    private UserServiceImpl userService;

    @RequestMapping(value = "/system/user/update",method = {RequestMethod.POST})
    public String userUpdate(@ModelAttribute("user")User user, Model model){
        Map<String, MsgDto> msg=new HashMap<>();
        if(user.getAccount()==null || user.getAccount()==""){
            msg.put("account",new MsgDto("account","账号不能为空"));
        }
        if(user.getPassword()==null || user.getPassword()==""){
            msg.put("password",new MsgDto("password","密码不为空"));
        }
        if(user.getRoles()==null || user.getRoles()==""){
            msg.put("roles",new MsgDto("roles","角色不为空"));
        }

        try{
            if(msg.isEmpty()){
                userService.updateUser(user);
                System.out.println("更新成功");
            }
        }catch (Exception e){
            model.addAttribute("error",new MsgDto("","更新失败"));
        }

        model.addAttribute("mag",msg);
        model.addAttribute("user",user);
        // 返回查询用户
        return "redirect:system/user/query";
    }



    @RequestMapping(value = "/system/user/delete/{id}",method = RequestMethod.POST)
    public String userDelete(@PathVariable int id, Model model){
        try{
            userService.deleteUser(id);
            model.addAttribute("success",new MsgDto("","删除成功"));
        }catch (Exception e){
            model.addAttribute("error",new MsgDto("","删除失败")+e.getMessage());
        }

        return "redirect:system/user/query";
    }

}
